#ifndef LCD1602_H
#define LCD1602_H

#define LCD1602 P0

sbit LCD_RS = P2^5;
sbit LCD_RW = P2^6;
sbit LCD_EN = P2^7;

#define LCD_RS_0 LCD_RS=0
#define LCD_RS_1 LCD_RS=1
#define LCD_RW_0 LCD_RW=0
#define LCD_RW_1 LCD_RW=1
#define LCD_EN_0 LCD_EN=0
#define LCD_EN_1 LCD_EN=1

void lcddelayms(unsigned int xms) 
{
	unsigned int x;
	unsigned char y;
	for(x = xms; x > 0; x--)
		for(y = 114; y>0; y--);//11.0592MHz: 114  //12MHz: 124
}

extern void _nop_ ( void );

#define DELAY_1602 lcddelayms(5)

void write1602_com(unsigned char com)
{
	LCD_RS_0;
	LCD1602 = com;
	DELAY_1602;
	LCD_EN_1;
	DELAY_1602;
	LCD_EN_0;
}

void write1602_dat(unsigned char dat)
{
	LCD_RS_1;
	LCD1602 = dat;
	DELAY_1602;
	LCD_EN_1;
	DELAY_1602;
	LCD_EN_0;
}

void write1602_string(unsigned char *dat,size)
{
	int i =0; 
	for(i=0;i<(size-1);i++)
	write1602_dat(*(dat++));
}

void init1602( void )
{
	LCD_RW_0;
	write1602_com(0x38);
	write1602_com(0x0C);
	write1602_com(0x06);
	write1602_com(0x01);
}

#endif